package com.tiger.web.controller.project;

import com.tiger.common.annotation.Log;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.enums.BusinessType;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.framework.web.service.TokenService;
import com.tiger.project.domain.ContractSponsorCredit;
import com.tiger.project.service.IContractSponsorCreditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 租金减免Controller
 *
 * @author standard
 * @date 2022-11-09
 */
@Api(tags = "租金减免")
@RestController
@RequestMapping("/system/credit")
public class ContractSponsorCreditController extends BaseController {
    @Autowired
    private IContractSponsorCreditService contractSponsorCreditService;

    /**
     * 查询租金减免列表
     */
    @ApiOperation(value = "查询租金减免列表")
    //@PreAuthorize("@ss.hasPermi('system:credit:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContractSponsorCredit contractSponsorCredit) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        contractSponsorCredit.setCompanyId(loginUser.getUser().getDeptId());
        startPage();
        List<ContractSponsorCredit> list = contractSponsorCreditService.selectContractSponsorCreditList(contractSponsorCredit);
        return getDataTable(list);
    }


    /**
     * 获取租金减免详细信息
     */
    @ApiOperation(value = "获取租金减免列详细信息")
    //@PreAuthorize("@ss.hasPermi('system:credit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(contractSponsorCreditService.selectContractSponsorCreditById(id));
    }

    /**
     * 新增租金减免
     */
    @ApiOperation(value = "新增租金减免")
    //@PreAuthorize("@ss.hasPermi('system:credit:add')")
//    @Log(title = "租金减免", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ContractSponsorCredit contractSponsorCredit) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        contractSponsorCredit.setCompanyId(loginUser.getUser().getDeptId());
        contractSponsorCredit.setCreateById(loginUser.getUserId());
        contractSponsorCredit.setCreateByName(loginUser.getUser().getNickName());
        return toAjax(contractSponsorCreditService.insertContractSponsorCredit(contractSponsorCredit));
    }

    /**
     * 修改租金减免
     */
    @ApiOperation(value = "修改租金减免")
    //@PreAuthorize("@ss.hasPermi('system:credit:edit')")
//    @Log(title = "租金减免", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ContractSponsorCredit contractSponsorCredit) {
        return toAjax(contractSponsorCreditService.updateContractSponsorCredit(contractSponsorCredit));
    }

    /**
     * 删除租金减免
     */
    @ApiOperation(value = "删除租金减免")
    //@PreAuthorize("@ss.hasPermi('system:credit:remove')")
//    @Log(title = "租金减免", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(contractSponsorCreditService.deleteContractSponsorCreditByIds(ids));
    }

    /**
     *  租金减免 选择账期
     */
    @ApiOperation(value = "租金减免 选择账期")
    @GetMapping("/selectRentPeriods")
    public AjaxResult selectRentPeriods(@ApiParam(value = "合同id") Long id){
        return AjaxResult.success(contractSponsorCreditService.selectRentPeriods(id));
    }

    /**
     * 开票信息
     */
    @ApiOperation(value = "开票信息")
    @GetMapping("/makeInvoice")
    public AjaxResult makeInvoice(@ApiParam(value = "合同id") Long contractSponsorId, @ApiParam(value = "租赁期数") Long rentPeriods){
        return AjaxResult.success(contractSponsorCreditService.makeInvoice(contractSponsorId, rentPeriods));
    }

    /**
     * 租金减免  OA审批
     */
    @ApiOperation(value = "租金减免 OA审批")
    @GetMapping("/callBack")
    public AjaxResult callBack(@ApiParam(value = "租金减免id") Long id,@ApiParam(value = "审核状态 3-审批通过，4-已驳回") Long status,
                               @ApiParam(value = "oa审批文件链接") String oaFile){
        return toAjax(contractSponsorCreditService.callBack(id, status, oaFile));
    }
}
